﻿using UnityEngine;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;

public static class TextReadAndWrite   {

    static public TextAsset LoadFromResources(string path)
    {
        return Resources.Load<TextAsset>(path);
    }
	
    static public byte[] LoadFromPersistenPath(string path)
    {
        Debugger.Log("Load : " + Application.persistentDataPath + "/" + path);
#if UNITY_EDITOR || (!UNITY_FLASH && !NETFX_CORE && !UNITY_WP8)
        FileInfo fi = new FileInfo(Application.persistentDataPath + "/" + path);
        if (!fi.Exists) return null;

        FileStream fs = File.OpenRead(Application.persistentDataPath + "/" + path);

        if (fs != null)
        {
            fs.Seek(0, SeekOrigin.End);
            byte[] buffer = new byte[fs.Position];
            fs.Seek(0, SeekOrigin.Begin);
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();
            return Decode(buffer);
        }
#endif
        return null;
    }

    static public void WriteToPersistentPath(string path, byte[] bytes)
    {
        Debugger.Log("Write : " + Application.persistentDataPath + "/" + path);
#if UNITY_EDITOR || (!UNITY_FLASH && !NETFX_CORE && !UNITY_WP8)
        FileInfo fi = new FileInfo(Application.persistentDataPath + "/" + path);
        FileStream fs = null;
        if (!fi.Exists)
            fs = fi.Create();
        else
            fs = File.OpenWrite(Application.persistentDataPath + "/" + path);

        if (fs != null)
        {
            fs.Seek(0, SeekOrigin.Begin);
            if (bytes == null || bytes.Length == 0)
                fs.SetLength(0);
            else
            {
                byte[] encode = Encode(bytes);
                fs.Write(encode, 0, encode.Length);
            }
            fs.Close();
        }
#endif
    }

    static public void DeleteFromPersistenPath(string path)
    {
        FileInfo fi = new FileInfo(Application.persistentDataPath + "/" + path);
        if (!fi.Exists) return;

        Debugger.Log("Delete : " + Application.persistentDataPath + "/" + path);
        fi.Delete();
    }

    static public string ConvertBytesToString(byte[] bytes)
    {
        return System.Text.Encoding.UTF8.GetString(bytes);
    }

    static public byte[] Encode(byte[] bytes)
    {
        if (bytes == null || bytes.Length == 0) return bytes;
        DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();//实例化数据加密标准
        MemoryStream mStream = new MemoryStream();//实例化内存流
        //将数据流链接到加密转换的流
        CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
        cStream.Write(bytes, 0, bytes.Length);
        cStream.FlushFinalBlock();
        return mStream.ToArray();
    }

    static public byte[] Decode(byte[] bytes)
    {
        if (bytes == null || bytes.Length == 0) return bytes;
        DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
        MemoryStream mStream = new MemoryStream();
        CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
        cStream.Write(bytes, 0, bytes.Length);
        cStream.FlushFinalBlock();
        return mStream.ToArray();
    }

    static readonly byte[] rgbKey = new byte[] { 0x20, 0x14, 0x08, 0x01, 0x17, 0x46, 0x06, 0xCD };
    static readonly byte[] rgbIV = new byte[] { 0x22, 0x16, 0x09, 0x03, 0x15, 0x47, 0x16, 0x1D };
}
